<?php
/**
 * Created by PhpStorm.
 * Login: Administrator
 * Date: 2017-10-25
 * Time: 14:30
 */
namespace app\index\controller;
use think\Controller;
use think\Db;

class Region extends Base
{
    public function index()
    {//主体列表
        $this->baseindex();
        $region = DB::table('oa_region r')->field('r.region,r.id,u.ch_name,r.number')->join('oa_user u', "r.user_id=u.id", "left")->where('r.state=1')->select();
        $this->assign('region', $region);
        return $this->fetch();
    }

    public function regionadd()
    {//添加页面
        $this->baseindex();
        return $this->fetch();
    }

    public function regionadd_sub()
    {
        //处理添加提交数据
        $this->baseindex();
        //return 6;
        $num = '';
        if (isset($_POST['sub']) && $_POST['sub'] == 'submit') {
            //return 5;
            $data = input('post.data');
            $res = json_decode($data, true);
            $ch_name['ch_name'] = $res['ch_name'];
            //return $ch_name;
            $where['region'] = $res['region'];
            $where['number'] = $res['number'];
            //return $where['number'];
            $number = $this->number($where['number']);
            //return $number;
            if ($number) {
                $user_id = Db::table('oa_user')->field('id')->where($ch_name)->find();
                if (!empty($user_id)) {
                    $where['user_id'] = $user_id['id'];
                    if (!empty($where['region']) && !empty($where['number'])) {
                        $region = Db::table('oa_region')->where("region='{$where['region']}'")->find();
                        if (empty($region)) {
                            $num = Db::table('oa_region')->insert($where);
                            return $num;
                        } else {
                            $num = 3;
                            return $num;
                        }
                    } else {
                        return $num;
                    }
                } else {
                    if (!empty($where['region']) && !empty($where['number'])) {
                        $region = Db::table('oa_region')->where("region='{$where['region']}'")->find();
                        if (empty($region)) {
                            $num = Db::table('oa_region')->insert($where);
                            $num = $num + 1;
                            return $num;
                        } else {
                            $num = 3;
                            return $num;
                        }
                    } else {
                        return $num;
                    }
                }

            } else {
                return $num;
            }

        } else {
            return $num;
        }
    }

    public function regiondel()
    {//删除
        $this->baseindex();
        if (isset($_POST['id'])) {
            $id = $_POST['id'];
            $num = DB::table('oa_region')->where('id=' . $id)->update(['state' => 0]);
            return $num;
        }
    }

    public function regionsave()
    {//修改页面数据
        $this->baseindex();
        if (isset($_GET['id'])) {
            $id = input('get.id');
            $region = Db::table('oa_region r')->field('r.region,r.id,r.number,u.ch_name')
                ->join('oa_user u', 'r.user_id=u.id', 'left')->where('r.id=' . $id)->find();
            $this->assign('region', $region);
            return $this->fetch();
        }
        if (isset($_POST['sub']) && $_POST['sub'] == 'submit') {
            //修改数据处理
            //return 4;
            $data = input('post.data');
            $res = json_decode($data, true);
            $ch_name['ch_name'] = $res['ch_name'];
            $region['region'] = $res['region'];
            $where['region'] = $res['region'];
            $where['number'] = $res['number'];
            $region_id = $res['id'];
            $num = '';
            $yan_num = $this->number($where['number']);
            if ($yan_num) {
                if (!empty($region)) {
                    $region_res = Db::table('oa_region')->where($region)->select();
                    if (!empty($region_res)) {
                        if (count($region_res) == 1) {
                            if ($region_id == $region_res[0]['id']) {
                                if (!empty($ch_name)) {
                                    $user_id = Db::table('oa_user')->field('id')->where($ch_name)->find();
                                    if (!empty($user_id)) {
                                        $where['user_id'] = $user_id['id'];
                                        Db::table('oa_region')->where('id=' . $region_id)->update($where);
                                        return 1;
                                    } else {
                                        $where['user_id'] = 0;
                                        $result=Db::table('oa_region')->where($where)->find();
                                        if ($result['id'] == $region_id){
                                            return 2;
                                        }else{
                                            $num=Db::table('oa_region')->where('id=' . $region_id)->update($where);
                                            if ($num){
                                                return 2;
                                            }else{
                                                return $num;
                                            }
                                        }
                                    }
                                } else {
                                    $where['user_id'] = 0;
                                    $num=Db::table('oa_region')->where('id=' . $region_id)->update($where);
                                    if ($num){
                                        return 2;
                                    }else{
                                        return $num;
                                    }
                                }
                            } else {
                                return 3;
                            }
                        } else {
                            return $num;
                        }
                    } else {
                        if (!empty($ch_name)) {
                            $user_id = Db::table('oa_user')->field('id')->where($ch_name)->find();
                            if (!empty($user_id)) {
                                $where['user_id'] = $user_id['id'];
                                $num = Db::table('oa_region')->where('id=' . $region_id)->update($where);
                                return $num;
                            } else {
                                $where['user_id'] = 0;
                                $num = Db::table('oa_region')->where('id=' . $region_id)->update($where);
                                if ($num){
                                    return 2;
                                }else{
                                    return $num;
                                }
                            }
                        } else {
                            $where['user_id'] = 0;
                            $num = Db::table('oa_region')->where('id=' . $region_id)->update($where);
                            if ($num){
                                return 2;
                            }else{
                                return $num;
                            }
                        }
                    }
                } else {
                    return $num;
                }
            } else {
                return $num;
            }
        }
    }

    public function regionlist()
    {
        //主体分页数据
        $this->baseindex();
        $page = input('get.curr');
        $num = input('get.nums');
        $count = Db::table('oa_region')->where('state=1')->count();
        $res = Db::table('oa_region r')->field('r.id,r.region,r.number,u.ch_name')
            ->join('oa_user u', 'r.user_id=u.id', 'left')->where('r.state=1')->page($page, $num)->select();
        $date['code'] = 0;
        $date['msg'] = '';
        $date['count'] = $count;
        $date['data'] = $res;
        return $date;
    }
}